Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where session cookie was not always written at the right time. #1160

Merged
merged 5 commits into from
May 31, 2020
Merged

Fix bug where session cookie was not always written at the right time. #1160

merged 5 commits into from
May 31, 2020

Conversation

wout
Copy link
Contributor

@wout wout commented May 29, 2020

Purpose

This fixes #1064.

Description

  • Lucky::SessionHandler has been removed
  • Lucky::FlashHandler has been removed
  • flash messages, session cookie and cookies are now written in Lucky::TextResponse#print

I tested this on our app as well and everything works as expected now. 😌️

Checklist

  • - An issue already exists detailing the issue/or feature request that this PR fixes
  • - All specs are formatted with crystal tool format spec src
  • - Inline documentation has been added and/or updated
  • - Lucky builds on docker with ./script/setup
  • - All builds and specs pass on docker with ./script/test

@wout
Copy link
Contributor Author

wout commented May 29, 2020

We'll also need to update Lucky CLI to remove Lucky::SessionHandler from the generator. I can take care of that as well if you like.

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So glad you found the issue! This looks great to me. I just left a few small comments, but then I think it's good to go.

src/lucky/text_response.cr Show resolved Hide resolved
@@ -196,7 +198,7 @@ module Lucky::Renderable
{% raise "'text' in actions has been renamed to 'plain_text'" %}
end

@[Deprecated("`render_text deprecated. Use `plain_text` instead")]
@[Deprecated("`render_text` deprecated. Use `plain_text` instead")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -67,6 +159,11 @@ private def print_response(context : HTTP::Server::Context, status : Int32?)
print_response_with_body(context, "", status)
end

private def print_response_with_body(context : HTTP::Server::Context, body = "", status = 200, content_type = "text/html")
private def print_response_with_body(
context : HTTP::Server::Context,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the formatter do this? I ask because the one below has the first arg start on the same line as the signature. I'm thinking maybe we make them the same (one way or the other). I was curious if the formatter allowed both, or if it prefers one vs the other way. Does that make sense?

Copy link
Contributor Author

@wout wout May 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to put the first argument on a new line if everything does not fit on the same line. But the formatter does respect that and will put all following arguments on the same indentation level.

I like it because that will consistently put all multi-line arguments on the same indentation throughout the file. Otherwise, they are hanging at different places, sometimes with odd-numbered indentation levels, depending on the length of the name of the method.

Here again, if you prefer the first argument at the same line as the method, I'll change that. ☺️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I have been moving a lot of code around there, because that's where I initially added the changes. But then I realised it should be somewhere else. That's why it has been changed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this look good 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be cleaner indeed, so I added those changes as well.

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just need to handle flash and I think it is good to go. Thanks for investigating + fixing.

Do you know why it wasn't working before, but it worked this time? Was it that it changed to the TextResponse class? Just trying to make sure I understand why this works but it didn't work when done in Renderable

@@ -20,6 +20,8 @@ class Lucky::TextResponse < Lucky::Response
end

def print : Nil
write_session
write_cookies
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that cookies/session are set here, I think the flash handler should also be removed and the flash set before write_session. That way it gets written to the session cookie when it is printed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I'll do that as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Voila, that's done!

@@ -67,6 +159,11 @@ private def print_response(context : HTTP::Server::Context, status : Int32?)
print_response_with_body(context, "", status)
end

private def print_response_with_body(context : HTTP::Server::Context, body = "", status = 200, content_type = "text/html")
private def print_response_with_body(
context : HTTP::Server::Context,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this look good 👍

@wout
Copy link
Contributor Author

wout commented May 29, 2020

Do you know why it wasn't working before, but it worked this time? Was it that it changed to the TextResponse class? Just trying to make sure I understand why this works but it didn't work when done in Renderable

Well, yeah, that's what threw me off. I didn't realise Lucky::TextResponse is initialized in two places:

So I added the write_session and write_cookies methods to the latter, not the former. It makes sense to move those methods to a more central place, especially since it's also used in other places:

https://github.com/wout/lucky/blob/1064-write-cookies-right-before-text-response/src/lucky/redirectable.cr#L62-L81

@paulcsmith
Copy link
Member

Ohhh that makes total sense. And I definitely agree on adding it to TextResponse so all the responses get it. Have you tested this new update with your flash messages and can confirm those are still working as expected? If so I think this LGTM!

@wout
Copy link
Contributor Author

wout commented May 29, 2020

I'll have to look closer at it tomorrow. Specs run green, but not sure about my apps.

@paulcsmith
Copy link
Member

Sounds good. The flash/session stuff can be a bit gnarly with edge cases so better safe than sorry 😂

Thanks for double checking and for fixing this. Can't wait to get this out

@wout
Copy link
Contributor Author

wout commented May 29, 2020

Yeah, flash messages are a breed of their own. :) I'll have to install it into an app that uses them. More tomorrow!

@wout
Copy link
Contributor Author

wout commented May 30, 2020

@paulcsmith I've tested flash messages throughout one app and they all work as expected, except for the following two scenarios.

  1. a flash message set in the SignIns::Delete action is lost
    But I recall noticing this before. In one of my apps I even removed the default flash message there.

  2. flash messages are not maintained with multiple redirects
    I'm not sure if this was working before either.

@paulcsmith
Copy link
Member

I think this is ok to merge. Could you open issues for those 2 redirect issues? I think they are likely bugs from before that we need to fix.

Thanks for the thorough investigation, and for the fix!

@paulcsmith paulcsmith merged commit 02c3de6 into luckyframework:master May 31, 2020
@wout wout deleted the 1064-write-cookies-right-before-text-response branch May 31, 2020 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set-Cookie is not arrived to a browser
3 participants